package com.google.code.yargon.level;

import java.util.*;
import org.apache.log4j.Logger;
import com.google.code.yargon.util.LogUtil;

@SuppressWarnings("serial")
public class LocationSet extends LinkedList<Location> {

	private Logger log = LogUtil.getInstance();
	
	//protected LinkedList<Location> points = null;
	
	/**
	 * Construct a new location set
	 */
	public LocationSet() {
		super();
	}
	
	/**
	 * Construct a new location set
	 */
	public LocationSet(LocationSet ls) {
		super(ls);
	}
		
	/**
	 * Push new way point to the route by coordinates
	 * 
	 * @param x
	 * @param y
	 */
	public void push(int x, int y) {
		Location l = new Location(x, y, 0);
		push(l);
	}
	
	/**
	 * Push new way point to the route by Location
	 * 
	 * @param l
	 */
	public void push(Location l) {
		if(size() == 0) {		
			add(l);
			log.trace("New point ("+size()+"): "+l.getX()+","+l.getY());
			return;
		} 
		else {
			for(int i = 0; i < size(); i++) {
				if(get(i).equals(l)) {
					log.trace("New point equals existing, discarding");
					return;
				}
			}
			add(l);
			log.trace("New point ("+size()+"): "+l.getX()+","+l.getY());
		} 
	}
	
	/**
	 * Pop the last added way point
	 */
	public Location pop() {
		Location l = null;
		try {
			l = removeLast();
		} catch(Exception e) {
			log.warn("Tried to pop non-existant point!");
		}
		return l;
	}
		
	/**
	 * Return all way points as a list
	 * @return
	 */
	public List<Location> getPoints() {
		return this;
	}
		
	/** 
	 * Return a string containing all way point coordinates
	 * 
	 */
	public String toString() {
		String str = "";
		for(Location l : this) {
			str += l.toString()+", ";
		}
		return str;
	}
	
	/** 
	 * Get a copy of this location set (new object)
	 * @return
	 */
	public LocationSet getCopy() {
		return (LocationSet) new LocationSet(this);
	}
}
